﻿<Assembly: WebResource("YCDotNetFramework.YCTextBox.js", "application/x-javascript")> 

Namespace Controle

    Public Class YCTextBox
        Inherits TextBox

#Region "Variaveis Private"

        Private pTipo As EnumTipo = EnumTipo.Normal
        Private pMostrarCalendario As Boolean = False

#End Region

#Region "Public Property"

        Public Enum EnumTipo
            Normal
            Hora
            Calendario
            MesAno
            Ano
            CPF
            CPFCNPJ
            CEP
            Telefone
            [Decimal]
            SoNumeros
        End Enum

        Public Property Tipo() As EnumTipo
            Get
                Return pTipo
            End Get
            Set(ByVal value As EnumTipo)
                pTipo = value
            End Set
        End Property

        Public Overrides Property Width() As System.Web.UI.WebControls.Unit
            Get
                Return MyBase.Width
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                If Me.Width = Nothing Then
                    MyBase.Width = value
                Else
                    MyBase.Width = Me.Width
                End If
            End Set
        End Property

        Public Property MostrarCalendario() As Boolean
            Get
                Return pMostrarCalendario
            End Get
            Set(ByVal value As Boolean)
                pMostrarCalendario = value
            End Set
        End Property

        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(ByVal value As String)
                MyBase.Text = FormatarTexto(value, Me.Tipo)
            End Set
        End Property

        Public Overloads Property Text(ByVal Formatar As Boolean) As Object
            Get
                Select Case Tipo
                    Case EnumTipo.SoNumeros, EnumTipo.CPF, EnumTipo.CPFCNPJ, EnumTipo.Telefone, EnumTipo.CEP
                        Return Regex.Replace(MyBase.Text, "[^\d]", String.Empty)
                    Case Else
                        Return MyBase.Text
                End Select
            End Get
            Set(ByVal value)
                MyBase.Text = FormatarTexto(value, Tipo)
            End Set
        End Property

#End Region

#Region "Métodos Publicos"

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            Page.ClientScript.RegisterClientScriptResource(GetType(YCTextBox), "YCDotNetFramework.YCTextBox.js")

            Select Case Me.TextMode
                Case TextBoxMode.SingleLine
                    Select Case Tipo
                        Case EnumTipo.Normal

                        Case EnumTipo.Hora
                            Me.Width = "35"
                            Me.MaxLength = 5

                        Case EnumTipo.Calendario
                            If pMostrarCalendario Then
                                Me.Attributes.Add("onclick", "DefineData(this.id, new Date().getDate(), (new Date().getMonth()+1), new Date().getFullYear());")
                            Else
                                Me.Attributes.Add("onblur", "IsDate(this)")
                            End If
                            Me.Attributes.Add("onkeydown", "MascaraCalendario(this, event);")
                            Me.Attributes.Add("onkeyup", "MascaraCalendario(this, event);")
                            Me.Width = "70"
                            Me.MaxLength = 10

                        Case EnumTipo.MesAno
                            Me.Attributes.Add("onkeydown", "MascaraMesAno(this, event);")
                            Me.Attributes.Add("onkeyup", "MascaraMesAno(this, event);")
                            Me.Attributes.Add("onblur", "IsMesAno(this);")
                            Me.MaxLength = 7
                            Me.Width = 55

                        Case EnumTipo.Ano
                            Me.MaxLength = 4
                            Me.Width = 35
                            Me.Attributes.Add("onblur", "if(this.value != '' && (this.value <= 1900 || this.value >= 2050)){alert('Ano inválido');this.value='';}")

                        Case EnumTipo.CPF
                            Me.Attributes.Add("onkeypress", "MascaraCpf(this, event);")
                            Me.MaxLength = 14
                            Me.Width = 90

                        Case EnumTipo.CPFCNPJ
                            Me.Width = 105
                            Me.MaxLength = 18

                        Case EnumTipo.Telefone
                            Me.Attributes.Add("onkeypress", "MascaraTelefone(this, event);")

                            Me.MaxLength = 13
                            Me.Width = 85
                        Case EnumTipo.CEP
                            Me.Attributes.Add("onkeyup", "MascaraCEP(this, event);")
                            Me.Attributes.Add("onkeydown", "MascaraCEP(this, event);")
                            Me.Width = 70
                            Me.MaxLength = 10

                        Case EnumTipo.SoNumeros
                            Me.Attributes.Add("onkeypress", "MascaraSoNumeros(this, event);")
                            Me.Width = 70
                            If Me.MaxLength = 0 Then Me.MaxLength = 9

                        Case EnumTipo.Decimal
                            Me.Width = 70
                            Me.Attributes.Add("onkeyup", "MascaraDecimalMonetario(this, event);")
                            Me.Attributes.Add("onkeydown", "MascaraDecimalMonetario(this, event);")
                            If Me.MaxLength = 0 Then
                                Me.MaxLength = 9
                            Else
                                Me.MaxLength = Me.MaxLength + 3
                            End If
                    End Select

                Case TextBoxMode.Password
                    Me.Attributes.Add("senha", MyBase.Text)
                    Web.Javascript.Executa(String.Concat("$get('", Me.ClientID, "').value=$get('", Me.ClientID, "').getAttribute('senha');"))
                Case TextBoxMode.MultiLine
                    If Me.MaxLength > 0 Then
                        Me.Attributes.Add("onkeyup", "MaxLengthTextArea(this, " & Me.MaxLength & ")")
                        Me.Attributes.Add("onkeydown", "MaxLengthTextArea(this, " & Me.MaxLength & ")")
                    End If
            End Select
        End Sub

        Public Shared Function FormatarTexto(ByVal str As String, ByVal Tipo As EnumTipo) As String
            Select Case Tipo
                Case EnumTipo.Telefone
                    Return Regex.Replace(String.Format("{0,-10}", str), "([\d]{2})([\d]{4})([\d]{4})", "($1)$2-$3")
                Case EnumTipo.CPF
                    Return Regex.Replace(String.Format("{0,-11}", str), "(\d{3})(\d{3})(\d{3})(\d{2})", "$1.$2.$3-$4")
                Case EnumTipo.CEP
                    Return Regex.Replace(String.Format("{0,-8}", str), "([\d]{2})([\d]{3})([\d]{3})", "$1.$2-$3")
                Case EnumTipo.Calendario
                    If String.IsNullOrEmpty(str) Then
                        Return str
                    Else
                        Return String.Format("{0:dd/MM/yyyy}", CDate(str))
                    End If
                Case Else
                    Return str
            End Select
        End Function

#End Region

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.Render(writer)
            Select Case Tipo
                Case EnumTipo.Calendario
                    'writer.Write(String.Format("<img src=""{0}"" />", Page.ClientScript.GetWebResourceUrl(GetType(YCTextBox), "YCFramework.Calendario.png"))) TODO: 
            End Select
        End Sub
    End Class

End Namespace
